package com.hanbang.taitaigou.ui.widget.dialog;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hanbang.taitaigou.R;
import com.hanbang.taitaigou.ui.widget.MaxHeightListView;
import com.hanbang.taitaigou.ui.widget.timeselete.DateWheelMain;
import com.hanbang.taitaigou.ui.widget.timeselete.WheelView;
import com.hanbang.taitaigou.utils.annotation.SelectItem;
import com.hanbang.taitaigou.utils.http.JsonHelper;
import com.hanbang.taitaigou.utils.other.ObjectUtils;
import com.hanbang.taitaigou.utils.ui.LogUtils;
import com.hanbang.taitaigou.utils.ui.UiUtils;


/*
 * 显示整个项目中要显示对话框
 * MODE: LoaddingDialog：加载对话框
 */
public class BaseDialog extends Dialog {

    private int titleColor = 0;

    private ProgressBar progressBar = null;
    private TextView mbTv = null;
    private TextView biliTv = null;
    private MaxHeightListView listView = null;
    private ArrayList<? extends Object> listData = null;
    private DateWheelMain wheelMain = null;
    private boolean isAutoDismiss = true;

    /*
     * 对话框的类型 枚举 LOADDING :加载对话框
     */
    public static enum MODE {
        LOADDING, CONFIRM, HINT, HEAD, EDITTEXT, PROGRESS, SELECT, DATE
    }

    ;

    private MODE mode = null;
    private Context context;
    // 对话框点击事件
    private OnClickCallback onClickCallback = null;

    public BaseDialog(Context context, MODE mode) {
        this(context, R.style.base_dialog, mode);
    }

    public BaseDialog(Context context, int theme, MODE mode) {
        super(context, theme);
        titleColor = context.getResources().getColor(R.color.black);
        this.mode = mode;
        this.context = context;
        switch (mode) {
            case LOADDING:
                // R.style.loadding_dialog
                setContentView(R.layout.dialog_loadding);
                getWindow().getAttributes().gravity = Gravity.CENTER;
                break;
            case CONFIRM:
                setContentView(R.layout.dialog_confirm);
                getWindow().getAttributes().gravity = Gravity.CENTER;
                break;
            case HINT:
                setContentView(R.layout.dialog_hint);
                getWindow().getAttributes().gravity = Gravity.CENTER;
                break;
            case HEAD:
                setContentView(R.layout.dialog_about_me);
                getWindow().getAttributes().gravity = Gravity.BOTTOM;
                break;
            case EDITTEXT:
                setContentView(R.layout.dialog_edittext);
                getWindow().getAttributes().gravity = Gravity.CENTER;
                break;
            case PROGRESS:
                setContentView(R.layout.dialog_progress);
                getWindow().getAttributes().gravity = Gravity.CENTER;
                setCancelable(false);
                break;
            case SELECT:
                setContentView(R.layout.dialog_select);
                getWindow().getAttributes().gravity = Gravity.CENTER;
                break;
            case DATE:
                setContentView(R.layout.dialog_datepicker);
                getWindow().getAttributes().gravity = Gravity.CENTER;
                break;
            default:
                break;
        }
    }

    public BaseDialog(Context context) {
        super(context);
    }

    @Override
    public void show() {
        // 判断是否加载对话框
        if (context instanceof Activity) {
            if (((Activity) context).isFinishing()) {
                return;
            }
        }
        super.show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置字体的主题颜色 ,候回调接口
        TextView confirm = ((TextView) findViewById(R.id.confirm));
        if (confirm != null) {
            confirm.setTextColor(titleColor);
            confirm.setOnClickListener(onClickListener);
        }

        TextView title = ((TextView) findViewById(R.id.title));
        if (title != null) {
            title.setTextColor(titleColor);
        }

        TextView quxiao = ((TextView) findViewById(R.id.quxiao));
        if (quxiao != null) {
            if (mode == MODE.PROGRESS) {
                quxiao.setTextColor(titleColor);
            }
            quxiao.setOnClickListener(onClickListener);
        }

        switch (mode) {
            case HEAD:
                findViewById(R.id.about_me_dialog_t1).setOnClickListener(
                        onClickListener);
                findViewById(R.id.about_me_dialog_t2).setOnClickListener(
                        onClickListener);
                findViewById(R.id.about_me_dialog_t3).setOnClickListener(
                        onClickListener);
                break;
            case PROGRESS:
                mbTv = (TextView) findViewById(R.id.mb);
                biliTv = (TextView) findViewById(R.id.bili);
                progressBar = (ProgressBar) findViewById(R.id.progressBar1);
                break;
            case SELECT:
                listView = (MaxHeightListView) findViewById(R.id.listview);
                listView.setListViewHeight(ObjectUtils.dip2px(context, 300));
                listView.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        if (onClickCallback != null && listData != null) {
                            onClickCallback.onClick(BaseDialog.this, position,
                                    listData.get(position));
                        }
                        dismiss();
                    }
                });
                if (listData != null) {
                    listView.setAdapter(new MyAdapter());
                }
                break;
            case DATE:
                wheelMain = new DateWheelMain((Activity) context,
                        (WheelView) findViewById(R.id.year),
                        (WheelView) findViewById(R.id.month),
                        (WheelView) findViewById(R.id.day),
                        (WheelView) findViewById(R.id.hour),
                        (WheelView) findViewById(R.id.min));
                break;
            default:
                break;
        }

        UiUtils.applyFont(getWindow().getDecorView().findViewById(
                android.R.id.content));

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        /*
         * 加载对话框 旋转动画
		 */
        if (mode == MODE.LOADDING) {
            ImageView imageView = (ImageView) findViewById(R.id.image);
            Animation animation = AnimationUtils.loadAnimation(context,
                    R.anim.loadding_dialog_round);
            imageView.startAnimation(animation);
        }
    }

    /*
     * **********************对话框点击事件********************
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (onClickCallback != null) {
            onClickCallback.onClick(BaseDialog.this, -1, "dismiss");
        }

    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if (onClickCallback != null) {
                switch (v.getId()) {
                    case R.id.confirm:
                        if (mode == MODE.DATE) {
                            if (wheelMain != null) {
                                onClickCallback.onClick(BaseDialog.this, 2,
                                        wheelMain.getTime());
                            }
                        } else {
                            TextView view = (TextView) findViewById(R.id.content);
                            String c = null;
                            if (view != null) {
                                c = view.getText().toString();
                            }
                            onClickCallback.onClick(BaseDialog.this, 2, c);
                        }

                        dismissOnClick();
                        break;
                    case R.id.quxiao:
                        onClickCallback.onClick(BaseDialog.this, 1, "quxiao");
                        dismissOnClick();
                        break;
                    case R.id.about_me_dialog_t1:
                        onClickCallback.onClick(BaseDialog.this, 1, null);
                        dismissOnClick();
                        break;
                    case R.id.about_me_dialog_t2:
                        onClickCallback.onClick(BaseDialog.this, 2, null);
                        dismissOnClick();
                        break;
                    case R.id.about_me_dialog_t3:
                        onClickCallback.onClick(BaseDialog.this, 3, null);
                        dismissOnClick();
                        break;

                    default:
                        break;
                }
            } else {
                dismissOnClick();
            }

        }
    };

    public int getMainColor() {
        return titleColor;
    }

    public void setMainColor(int titleColor) {
        this.titleColor = titleColor;
    }



    public void setOnClickCallback(OnClickCallback callback) {
        onClickCallback = callback;
    }

    /*
     * **********************进度条对话框设置****************
     */
    public BaseDialog setProgress(int c) {
        if (progressBar != null && biliTv != null) {
            progressBar.setProgress(c);
            biliTv.setText(c + "%");
        }
        return this;
    }

    public BaseDialog setMb(Float count, Float current) {
        if (progressBar != null && mbTv != null) {
            mbTv.setText(current + "M/" + count + "M");
        }
        return this;
    }

	/*
     * **********************选择对话框设置****************
	 */

    public void setSelectData(ArrayList<? extends Object> data) {
        listData = data;
        if (listView != null) {
            listView.setAdapter(new MyAdapter());
        }

    }

    /*
     * **********************对话框设置****************
     */
    /*
     * 获取内容控件
	 */
    public TextView getContentView() {
        TextView c = ((TextView) findViewById(R.id.content));
        return c;
    }

    /*
     * 设置内容
     */
    public BaseDialog setContent(String content) {
        TextView c = ((TextView) findViewById(R.id.content));
        if (c != null && content != null) {
            c.setText(content);
        }
        if (c != null && content == null) {
            c.setVisibility(View.GONE);
        }
        return this;
    }

    /*
     * 设置内容宿儒
     */
    public BaseDialog setContentInputType(int inputType) {
        EditText c = ((EditText) findViewById(R.id.content));
        if (c != null) {
            c.setInputType(inputType);
        }
        return this;
    }

    /*
     * 设置标题
     */
    public BaseDialog setTitleMain(CharSequence title) {
        TextView tt = (TextView) findViewById(R.id.title);
        if (tt != null && title != null) {
            tt.setText(title);
        }
        if (tt != null && title == null) {
            tt.setVisibility(View.GONE);
        }
        return this;
    }


    /*
     * 设置确定按钮 文字 默认确定
     */
    public BaseDialog setConfirm(String confirm) {
        TextView queding = ((TextView) findViewById(R.id.confirm));
        if (queding != null && confirm != null) {
            queding.setText(confirm);
        }
        return this;
    }

    /*
     * 设置取消按钮 文字 默认取消
     */
    public BaseDialog setQuxiao(String confirm) {
        TextView quxiao = ((TextView) findViewById(R.id.quxiao));
        if (quxiao != null && confirm != null) {
            quxiao.setText(confirm);
        }
        return this;
    }

    /*
     * 设置确定按钮 文字颜色
     */
    public BaseDialog setConfirmColor(int color) {
        TextView queding = ((TextView) findViewById(R.id.confirm));
        if (queding != null && color != 0) {
            queding.setTextColor(color);
        }
        return this;
    }

    /*
     * 设置取消按钮 文字 默认取消
     */
    public BaseDialog setQuxiaoColor(int color) {
        TextView quxiao = ((TextView) findViewById(R.id.quxiao));
        if (quxiao != null && color != 0) {
            quxiao.setTextColor(color);
        }
        return this;
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            if (listData == null) {
                return 0;
            } else {
                return listData.size();
            }
        }

        @Override
        public Object getItem(int position) {
            if (listData == null) {
                return null;
            } else {
                return listData.get(position);
            }
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @SuppressLint("InflateParams")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(
                        R.layout.dialog_item_select, null);
            }

            TextView textview = (TextView) convertView
                    .findViewById(R.id.textview);
            textview.setText(getSelectDialogItemName(listData.get(position)));
            return convertView;
        }

    }

    private String getSelectDialogItemName(Object obj) {
        if (obj instanceof String) {
            return obj.toString();
        }
        Class<? extends Object> clazz = obj.getClass();
        // 取出bean里的所有方法
        Method[] methods = clazz.getDeclaredMethods();
        Field[] fields = clazz.getDeclaredFields();
        for (Field f : fields) {
            // 判断这个字段是否 是需要的SelectItem
            if (f.getAnnotation(SelectItem.class) == null) {
                continue;
            }
            String getMetodName = JsonHelper
                    .parseMethodName(f.getName(), "get");
            if (!JsonHelper.haveMethod(methods, getMetodName)) {
                continue;
            }
            try {
                LogUtils.e("getSelectDialogItemName", getMetodName);
                Method fieldMethod = clazz.getMethod(getMetodName,
                        new Class[]{});
                LogUtils.e("getSelectDialogItemName",
                        (String) fieldMethod.invoke(obj));
                return (String) fieldMethod.invoke(obj);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    public void setAutoDismiss(boolean isAutoDismiss) {
        this.isAutoDismiss = isAutoDismiss;
    }

    public void dismissOnClick() {
        if (isAutoDismiss)
            super.dismiss();
    }
}
